<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page import="com.yaodian100.core.common.utils.ServerValue"%>
<head>
<style type="text/css">
h1 span{visibility:visible;}
td span{visibility:visible;}
.msg{float:left; line-height:1em; margin:0; display:block;border:1px solid #f66;padding:2px 5px;*padding:2px 5px 1px;}
.msg.rn{background-color:#fff2f2;border-color:#f66;}
</style>
</head>
<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath() %>'/>
<c-rt:set var="frontHostname" value='<%=ServerValue.getFrontHostname() %>'/>
<!-- 数字验证 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css" />
<script type="text/javascript" charset="UTF-8" src="/js/yaodian100.common.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.autotab-1.1b.js"></script>
<script language="javascript">
var isCorrect = true;
var needFocus;
	$().ready( function() {
		if('${error}'!=''){
			if('${error}'=='exist'){
				$("#dateTip").text('日期重复');
				$("#dateTip").show();
				needFocus = $("#saleDate");
			}else{
				var err = "${error}".split(',');
				for(var i=1; i<=err.length; i++){
					$("#tabTip_" + err[i-1]).text("商品编号不正确");
					$("#tabTip_" + err[i-1]).show();
					$("#productId_" + err[i-1]).focus();
				}
			}
		}
		$('#saleDate').datepick({minDate:new Date(<%=request.getAttribute("minDate").toString()%>), showOn:'both',numberOfMonths:2, buttonImageOnly: true, buttonImage: '/images/calendar.gif',dateFormat:"yy-mm-dd"});
		$(":input[id*='memberPrice_']").numeric({allow:"."});
		$(":input[id*='limitPrice_']").numeric({allow:"."});
		
		$(":input[id*='productId_']").each(function(){
			$("#" + this.id).numeric();
			$(this).blur(function(){
				$(this).val(prodPadLeft($(this).val()));
				getProductInfo4("${midContextPath}","productName_"+this.id.split('_')[1],"iidxProductName_"+this.id.split('_')[1],"memberPrice_"+this.id.split('_')[1],"prodIsOk_"+this.id.split('_')[1],$(this).val());
			});
		});

		$(":input[id*='adWord']").alphanumeric();
		$(":input[id*='adWord']").autotab_filter({ format: 'custom', pattern: '[^a-zA-Z0-9]' });
		
		$("[id*=prodImg]").eq(0).change(function(){checkImgSize(0,$("#img1Correct"));});
		$("[id*=prodImg]").eq(1).change(function(){checkImgSize(1,$("#img2Correct"));});
		$("[id*=prodImg]").eq(2).change(function(){checkImgSize(2,$("#img3Correct"));});
		$("[id*=prodImg]").eq(3).change(function(){checkImgSize(3,$("#img4Correct"));});
		$("[id*=prodImg]").eq(4).change(function(){checkImgSize(4,$("#img5Correct"));});
		$("[id*=prodImg]").eq(5).change(function(){checkImgSize(5,$("#img6Correct"));});
		$("[id*=prodImg]").eq(6).change(function(){checkImgSize(6,$("#img7Correct"));});
		$("[id*=prodImg]").eq(7).change(function(){checkImgSize(7,$("#img8Correct"));});
		$("[id*=prodImg]").eq(8).change(function(){checkImgSize(8,$("#img9Correct"));});
		$("[id*=prodImg]").eq(9).change(function(){checkImgSize(9,$("#img10Correct"));});
		$("[id*=prodImg]").eq(10).change(function(){checkImgSize(10,$("#img11Correct"));});
		$("[id*=prodImg]").eq(11).change(function(){checkImgSize(11,$("#img12Correct"));});
		$("[id*=prodImg]").eq(12).change(function(){checkImgSize(12,$("#img13Correct"));});
		$("[id*=prodImg]").eq(13).change(function(){checkImgSize(13,$("#img14Correct"));});
		$("[id*=prodImg]").eq(14).change(function(){checkImgSize(14,$("#img15Correct"));});
		$("[id*=prodImg]").eq(15).change(function(){checkImgSize(15,$("#img16Correct"));});
		$("[id*=prodImg]").eq(16).change(function(){checkImgSize(16,$("#img17Correct"));});
		$("[id*=prodImg]").eq(17).change(function(){checkImgSize(17,$("#img18Correct"));});
		$("[id*=prodImg]").eq(18).change(function(){checkImgSize(18,$("#img19Correct"));});
		$("[id*=prodImg]").eq(19).change(function(){checkImgSize(19,$("#img20Correct"));});
		$("[id*=prodImg]").eq(20).change(function(){checkImgSize(20,$("#img21Correct"));});
		$("[id*=prodImg]").eq(21).change(function(){checkImgSize(21,$("#img22Correct"));});
		$("[id*=prodImg]").eq(22).change(function(){checkImgSize(22,$("#img23Correct"));});
		$("[id*=prodImg]").eq(23).change(function(){checkImgSize(23,$("#img24Correct"));});
		
		$(":input[id*='cis_repeat_']").click(function(){
			var now = this.id.split('_')[2];
			if($(this).attr('checked')){
				isCorrect = true;
				checkPreData(now-1);
				if(isCorrect){
					$("#iroundTag_"+now).val($("#iroundTag_"+(now-1)).val());
					$("#productId_"+now).val($("#productId_"+(now-1)).val());
					$("#productName_"+now).val($("#productName_"+(now-1)).val());
					$("#iidxProductName_"+now).val($("#iidxProductName_"+(now-1)).val());
					$("#memberPrice_"+now).val($("#memberPrice_"+(now-1)).val());
					$("#limitPrice_"+now).val($("#limitPrice_"+(now-1)).val());
					$("#prodIsOk_"+now).val($("#prodIsOk_"+(now-1)).val());
					$("#cis_limit_buy_"+now).attr('checked',$("#cis_limit_buy_"+(now-1)).attr('checked'));
					$("#iadWord_"+now).val($("#iadWord_"+(now-1)).val());
					//$("[id*=prodImg]").eq(now-1).val($("[id*=prodImg]").eq(now-2).val());
					$("#hide_data1_"+now).hide();
					$("#hide_data2_"+now).hide();
				}else{
					$(this).attr('checked',false);
				}
			}else{
				$("#hide_data1_"+now).show();
				$("#hide_data2_"+now).show();
			}
		});
		
		$('#submit_button').click(function(){
			isCorrect = true;
			$("span:[id*=Tip]").hide();
			$("span:[id*=Tip]").text('');

			for( var i = 24; i >=1 ; i-- ){

				checkPreData(i);
				
				if($("#cis_repeat_"+i).attr('checked')){
					$("#is_repeat_"+i).val('Y');
				}else{
					$("#is_repeat_"+i).val('N');
				}
				if($("#cis_limit_buy_"+i).attr('checked')){
					$("#is_limit_buy_"+i).val('N');
				}else{
					$("#is_limit_buy_"+i).val('Y');
				}
				if((!$("#cis_repeat_"+i).attr('checked'))&&$("[id*=prodImg]").eq(i-1).val()!=''){
					$("#uploadImage_"+i).val($("[id*=prodImg]").eq(i-1).val());
				}else{
					$("#uploadImage_"+i).val('N');
				}
				if($("#iidxProductName_"+i).val()==''){
					$("#idxProductName_"+i).val('#');
				}else{
					$("#idxProductName_"+i).val($("#iidxProductName_"+i).val());
				}
				if($("#iroundTag_"+i).val()==''){
					$("#roundTag_"+i).val('#');
				}else{
					$("#roundTag_"+i).val($("#iroundTag_"+i).val());
				}
				if($("#iadWord_"+i).val()==''){
					$("#adWord_"+i).val('#');
				}else{
					$("#adWord_"+i).val($("#iadWord_"+i).val());
				}
			}

			if($("#name").val()==''){
				$("#nameTip").text('请输入活动名称');
				$("#nameTip").show();
				needFocus=$("#name");
				isCorrect = false;
			}

			if($("#saleDate").val()==''){
				$("#dateTip").text('请选择日期');
				$("#dateTip").show();
				needFocus = $("#saleDate");
				isCorrect = false;
			}
			
			if(isCorrect){
				$('#timeLimitSaleForm').submit();
			}else{
				needFocus.focus();
			}
		}); 
		
	});
function appendWord(origin, apptext){
	if(origin.text().length==0){
		origin.text(apptext);
	}else{
		origin.text(apptext+', '+origin.text());
	}	
}

function checkPreData(pre){
	$("#tabTip_"+pre).hide();
	$("#tabTip_"+pre).text('');
	if($("#cis_repeat_"+pre).attr('checked')){
		return;
	}
	if($("#limitPrice_"+pre).val()==''){
		appendWord($("#tabTip_"+pre),'请输入秒杀价');
		$("#tabTip_"+pre).show();
		needFocus=$("#limitPrice_"+pre);
		isCorrect = false;
	}

	if($("#memberPrice_"+pre).val()==''){
		appendWord($("#tabTip_"+pre),'请输入会员价');
		$("#tabTip_"+pre).show();
		needFocus=$("#memberPrice_"+pre);
		isCorrect = false;
	}

	if($("#limitPrice_"+pre).val()!=''&&$("#memberPrice_"+pre).val()!=''){
		if(parseFloat($("#limitPrice_"+pre).val())>=parseFloat($("#memberPrice_"+pre).val())){
			appendWord($("#tabTip_"+pre),'秒杀价需小于会员价');
			$("#tabTip_"+pre).show();
			needFocus=$("#limitPrice_"+pre);
			isCorrect = false;
		}
	}
	
	if($("#iroundTag_"+pre).val()!=''&&countLength($("#iroundTag_"+pre).val())>8){
		appendWord($("#tabTip_"+pre),'圆标超出长度限制');
		$("#tabTip_"+pre).show();
		needFocus=$("#iroundTag_"+pre);
		isCorrect = false;
	}
	if($("#productName_"+pre).val()==''){
		appendWord($("#tabTip_"+pre),'请输入商品名称');
		$("#tabTip_"+pre).show();
		needFocus=$("#productName_"+pre);
		isCorrect = false;
	}else if($("#productName_"+pre).val().length>40){
		appendWord($("#tabTip_"+pre),'商品名称超出40');
		$("#tabTip_"+pre).show();
		needFocus=$("#productName_"+pre);
		isCorrect = false;
	}
	if($("#iidxProductName_"+pre).val()!=''&&$("#iidxProductName_"+pre).val().length>16){
		appendWord($("#tabTip_"+pre),'首页商品名称超出16');
		$("#tabTip_"+pre).show();
		needFocus=$("#iidxProductName_"+pre);
		isCorrect = false;
	}
	
	if($("#productId_"+pre).val()==''){
		appendWord($("#tabTip_"+pre),'请输入商品编号');
		$("#tabTip_"+pre).show();
		needFocus=$("#productId_"+pre);
		isCorrect = false;
	//}else if($("#prodIsOk_"+pre).val()=='N'){
	//	appendWord($("#tabTip_"+pre),'请输入正确的商品编号');
	//	$("#tabTip_"+pre).show();
	//	needFocus=$("#productId_"+pre);
	//	isCorrect = false;
	}else if(pre!=1){
		if(($("#productId_"+pre).val()==$("#productId_"+(pre-1)).val())&&(!$("#cis_repeat_"+pre).attr('checked'))&&(!$("#cis_repeat_"+(pre-1)).attr('checked'))){
			appendWord($("#tabTip_"+pre),'请勾选重复上一档次');
			$("#tabTip_"+pre).show();
			needFocus=$("#cis_repeat_"+pre);
			isCorrect = false;
		}else if($("#cis_repeat_"+pre).attr('checked')&&($("#productId_"+pre).val()!=$("#productId_"+(pre-1)).val())){
			appendWord($("#tabTip_"+pre),'请取消勾选重复上一档次');
			$("#tabTip_"+pre).show();
			needFocus=$("#cis_repeat_"+pre);
			isCorrect = false;
		}
	}
	
	if($("#imagePath_"+pre).val()=='N'){
		if((!$("#cis_repeat_"+pre).attr('checked'))&&$("[id*=prodImg]").eq(pre-1).val()==''){
			appendWord($("#tabTip_"+pre),'请选择图片');
			$("#tabTip_"+pre).show();
			needFocus=$("[id*=prodImg]").eq(pre-1);
			isCorrect = false;
		}
	}

	if($("#img"+pre+"Correct").val()!='' 
		&& (!$("#cis_repeat_"+pre).attr('checked')) 
		&& $("[id*=prodImg]").eq(pre-1).val()!=''){
			appendWord($("#tabTip_"+pre),$("#img"+pre+"Correct").val());
			$("#tabTip_"+pre).show();
			needFocus=$("[id*=prodImg]").eq(pre-1);
			isCorrect = false;
	}
}
function countLength(word){
    var length=0;
    for(var cc=0;cc<word.length;cc++){
       var c = word.charCodeAt(cc);
      if (!(c>=32&&c<=126)) length++;
    }
    return length+word.length;
}
function checkImgSize(imgNo, imgMsgTag){
	checkImg2(document.timeLimitSaleForm.prodImg[imgNo],20,258,136,260,150,imgMsgTag,"");
}
</script>
<div style="color:red">
<s:actionerror/>
<s:actionmessage/>
<s:fielderror />
</div>
<h1>限时秒杀管理
<span class="f-link"><a href="${midContextPath}/timeLimitSale/search.do">回列表页</a></span></h1>
<s:form action="save.do" method="post" name="timeLimitSaleForm" id="timeLimitSaleForm" enctype="multipart/form-data">
	<table class="tb-data" width="100%">
		<tbody>
			<tr>
				<th>限时活动日期</th>
				<td><input type="text" id="saleDate" name="saleDate" value="${saleDate}" maxlength="10"/><br/><span id="dateTip" class="msg rn" style="display:none;"></span>
				<input type="hidden" name="id" value="${id}"/></td>
			</tr>
			<tr>
				<th>活动名称</th>
				<td><input type="text" id="name" name="name" value="${name}" size="30" maxlength="30"/><br/><span id="nameTip" class="msg rn" style="display:none;"></span></td>
			</tr>
			<tr>
				<th colspan="4">秒杀商品设定</th>
			</tr>
			<c-rt:forEach items="${timeLimitProdList}" var="timeLimitProd" varStatus="rows_index">  
			<tr>
				<th>商品${rows_index.count}<br/><fmt:formatNumber pattern="00" value="${rows_index.count-1}"/>:00~<fmt:formatNumber pattern="00" value="${rows_index.count}"/>:00</th>
				<td width="320">
					<input type="hidden" name="prodId" value="${timeLimitProd.id}"/>
					<b<c-rt:if test="${rows_index.count==1}"> style="display:none;"</c-rt:if>><input type="checkbox" id="cis_repeat_${rows_index.count}" <c-rt:if test="${timeLimitProd.isRepeat=='Y'}">checked</c-rt:if>/>重复上一档次&nbsp;</b>
					<input type="hidden" id="is_repeat_${rows_index.count}" name="is_repeat" value="${timeLimitProd.isRepeat}"/>
					<div id="hide_data1_${rows_index.count}"<c-rt:if test="${timeLimitProd.isRepeat=='Y'}"> style="display:none;"</c-rt:if>>
					<b><input type="checkbox" id="cis_limit_buy_${rows_index.count}" <c-rt:if test="${timeLimitProd.isLimitBuy=='N'}">checked</c-rt:if> />允许重复购买</b><br/>
					<input type="hidden" id="is_limit_buy_${rows_index.count}" name="is_limit_buy" value="${timeLimitProd.isLimitBuy}"/>
					<b>圆标:</b><input type="text" id="iroundTag_${rows_index.count}" value="${timeLimitProd.roundTag}" maxlength="8" size="20" /><span>(4个中文字, 8个位元)</span><br/>
					<input type="hidden" name="roundTag" id="roundTag_${rows_index.count}" value="${timeLimitProd.roundTag}"/>
					<c-rt:if test="${not empty timeLimitProd.imagePath && timeLimitProd.imagePath!='N'}">
						<img src="${timeLimitProd.imagePath}" width="258" height="188"/><br/>
					</c-rt:if>
					<b>图片:</b><s:file name="prodImg" value="" size="30" id="prodImg"></s:file><br/><span>图片尺寸:258x136 &lt;20K</span>
					<s:if test="null != fieldErrors['prodImg']">
					<br/>
					<p><span class="msg rn"><s:fielderror><s:param>prodImg</s:param></s:fielderror></span></p>
					</s:if>
					<br/>
					<input type="hidden" id="img${rows_index.count}Correct" value=""/>
					<input type="hidden" id="uploadImage_${rows_index.count}" name="uploadImage" value=""/>
					<input type="hidden" id="imagePath_${rows_index.count}" name="imagePath" value="<c-rt:if test="${empty timeLimitProd.imagePath}">N</c-rt:if>${timeLimitProd.imagePath}"/>
					<span id="tabTip_${rows_index.count}" class="msg rn" style="display:none;"></span>
					</div>
				</td>
				<td>
					<div id="hide_data2_${rows_index.count}"<c-rt:if test="${timeLimitProd.isRepeat=='Y'}"> style="display:none;"</c-rt:if>>
					<b>商品编号:</b><input type="text" name="productId" id="productId_${rows_index.count}" maxlength="8" size="10" value="${timeLimitProd.productId}" />&nbsp;
					<b>市场价:</b><input type="text" name="memberPrice" id="memberPrice_${rows_index.count}" value="${timeLimitProd.memberPrice}" maxlength="10" size="10" />&nbsp;
					<b>秒杀价:</b><input type="text" name="limitPrice" id="limitPrice_${rows_index.count}" value="${timeLimitProd.limitPrice}" maxlength="10" size="10" /><br/>
					<input type="hidden" id="prodIsOk_${rows_index.count}" value=""/>
					<b>商品名称:</b><input type="text" name="productName" id="productName_${rows_index.count}" value="${timeLimitProd.productName}" maxlength="40" size="60" /><span>40字內</span><br/>
					<b>首页显示名称:</b><input type="text" id="iidxProductName_${rows_index.count}" value="${timeLimitProd.idxProductName}" maxlength="16" size="40" /><span>16字內</span><br/>
					<input type="hidden" name="idxProductName" id="idxProductName_${rows_index.count}" value="${timeLimitProd.idxProductName}"/>
					<b>广告代码:</b><input type="text" id="iadWord_${rows_index.count}" value="${timeLimitProd.adWord}" maxlength="25" size="20" /><span>非必填。由英文字母、数字组成，最多25个半角</span><br/>
					<input type="hidden" name="adWord" id="adWord_${rows_index.count}" value="${timeLimitProd.adWord}"/>
					</div>
				</td>
			</tr>
			</c-rt:forEach>
		</tbody>
	</table>
	<input type="button" id="submit_button" value="储存"/>
</s:form>
<div class="f-link">
	<a href="${midContextPath}/timeLimitSale/search.do">回列表页</a>
</div>